<?php

namespace app\admin\command;

use app\admin\model\exchange\Order;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use app\admin\model\station\Station;


class Orderdelete extends Command
{
    protected function configure()
    {
        $this
            ->setName('odt')
            ->setDescription('更新订单数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $all_order_ids = [];
        $onehour = time()-3600;
        $twohour = time()-7200;
        //查询两个小时到一个小时之间的订单数据
        $all_order = Order::where('createtime','>=',$twohour)->where('createtime','<',$onehour)->select();
        $all_station = [];
        $all_station_tmp = Station::select();
        if($all_station_tmp){
            foreach ($all_station_tmp as $value){
                $all_station[$value->store_no] = $value;
            }
        }
//        var_dump($all_station);die;
        if($all_order){
            foreach ($all_order as $val){
                $all_order_store[$val->store_no][] = $val;
            }
            foreach ($all_order as $item){
                $order_limit_str = isset($all_station[$item->store_no]->order_limit) && $all_station[$item->store_no]->order_limit ? $all_station[$item->store_no]->order_limit : 10;
                $order_limit_arr = explode(',',$order_limit_str);
                shuffle($order_limit_arr);
                $order_limit = $order_limit_arr[0];
                $all_order_num = isset($all_order_store[$item->store_no]) ? count($all_order_store[$item->store_no]) : 1;
                $order_limit_rate = ($order_limit*100)/$all_order_num;
                $show_order = isset($all_station[$item->store_no]->show_order) ? $all_station[$item->store_no]->show_order : 1;
                $rand_num = mt_rand(1,100);
                if($rand_num<$order_limit_rate && !$show_order){
                    $all_order_ids[] =  $item->id;
                }
                unset($rand_num);

            }
            if($all_order_ids){
                Order::where('id','in',$all_order_ids)->update(['is_delete'=>1]);
            }
        }

    }



}

//一个小时执行一次
//cd /data0/web/store.leyinshuke.com  && php think odt >> /data0/wwwlogs/1Orderdelete.log 2>&1

